package com.yhd.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yhd
 * @createtime 2021/1/22 14:37
 */
@SpringBootApplication
public class DelayConfig {

	//延时交换机
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "spring.boot.test.delay.exchange";
    //路由键
    public static final String ROUTING_ORDER_CANCEL = "spring.boot.test.delay.routing";
    //延迟队列
    public static final String QUEUE_ORDER_CANCEL  = "spring.boot.test.delay.queue";
    // 延迟时间 单位：秒
    public static final int DELAY_TIME  = 10;

    @Bean //声明延迟队列
    public Queue delayQueue() {
        // 第一个参数是创建的queue的名字，第二个参数是是否支持持久化
        return new Queue(QUEUE_ORDER_CANCEL, true);
    }

    @Bean //声明延迟交换机
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(EXCHANGE_DIRECT_ORDER_CANCEL, "x-delayed-message", true, false, args);
    }

    @Bean //延迟交换机绑定延迟队列并设置路由键
    public Binding bindingDelay() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(ROUTING_ORDER_CANCEL).noargs();
    }
}